<?php

class model_pddCat extends L {

    /**
     * 描述 ：分页的查询语句拼接
     * 作者 ：Zoro.Zhu
    **/
    public static function searchSql($params = array()){
        $sql = "SELECT
             `catId`, `catName`, `parentId`, `level`
        FROM
            `pdd_cat`
        WHERE 1";
        
        $sql .= !empty($params['search']['catId']) ? " AND `catId` = '{$params['search']['catId']}' " : "";
        $sql .= !empty($params['search']['catName']) ? " AND `catName` LIKE '%{$params['search']['catName']}%' " : "";
        $sql .= !empty($params['search']['parentId']) ? " AND `parentId` = '{$params['search']['parentId']}' " : "";
        $sql .= !empty($params['search']['level']) ? " AND `level` = '{$params['search']['level']}' " : "";
        return $sql;
    }

    /**
     * 描述 ：执行修改数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function updateSql($data = array()){
        $sql = "UPDATE `pdd_cat` 
        SET 
            `catName` = '{$data['catName']}',
            `parentId` = '{$data['parentId']}',
            `level` = '{$data['level']}'
        WHERE
            `catId` = '{$data['catId']}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：执行插入数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function insertSql($data = array()){
        $sql = "INSERT INTO `pdd_cat`(
             `catName`, `parentId`, `level`
        ) VALUES (
             '{$data['catName']}', '{$data['parentId']}', '{$data['level']}'
        )";
        return L::sql($sql);
    }

    /**
     * 描述：根据主键获取数据
     * 作者：Zoro.Zhu
    **/
    public static function getKeyInfo($key){
        $sql = "SELECT
             `catId`, `catName`, `parentId`, `level`
        FROM
            `pdd_cat`
        WHERE 
            `catId` = '{$key}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：根据类目名获取pdd类目Id
     * 作者 ：Zoro.Zhu
    **/
    public static function getCatId($catName){
        //先精确匹配
        $sql = "SELECT
            `catId` 
        FROM
            `pdd_cat` 
        WHERE
            `catName` = '{$catName}' 
        AND `level` IN ( 3, 4 ) 
        ORDER BY  `level` DESC
        LIMIT 1";
        if($cat = L::sql($sql)){
            return $cat[0]['catId'];
        }
        //精确匹配没匹配到，再逐渐模糊匹配
        $sql = "SELECT
            `catId` 
        FROM
            `pdd_cat` 
        WHERE
            `catName` LIKE '%{$catName}' 
        AND `level` IN ( 3, 4 ) 
        ORDER BY  `level` DESC
        LIMIT 1";
        if($cat = L::sql($sql)){
            return $cat[0]['catId'];
        }
        $sql = "SELECT
            `catId` 
        FROM
            `pdd_cat` 
        WHERE
            `catName` LIKE '{$catName}%' 
        AND `level` IN ( 3, 4 ) 
        ORDER BY  `level` DESC
        LIMIT 1";
        if($cat = L::sql($sql)){
            return $cat[0]['catId'];
        }
        $sql = "SELECT
            `catId` 
        FROM
            `pdd_cat` 
        WHERE
            `catName` LIKE '%{$catName}%' 
        AND `level` IN ( 3, 4 ) 
        ORDER BY  `level` DESC
        LIMIT 1";
        if($cat = L::sql($sql)){
            return $cat[0]['catId'];
        }
        return false;
    }

    /**
     * 描述 ：获取子ID
     *
     * 作者 ：Zoro.Zhu
    **/
    public static function getSonCatId($catId){
        $sql = "SELECT
            `catId` 
        FROM
            `pdd_cat` 
        WHERE
            `parentId` = '{$catId}' 
        AND `level` = 4 
        LIMIT 1";
        if($cat = L::sql($sql)){
            return $cat[0]['catId'];
        }
        return $catId;
    }


    /**
     * 描述 ：获取拼多多类目列表
     * 作者 ：Zoro.Zhu
     **/
    public static function getCatList() {
        $sql = "SELECT
            `catId`, `catName`, `parentId`, `level`
        FROM
            `pdd_cat`
        WHERE 1 ";
        $list = L::sql($sql);
        $crtData = $data = [];
        foreach ($list as $row){
            $data[$row['level']][$row['catId']] = $row['parentId'];
            $name[$row['catId']] = $row['catName'];
        }
        foreach($data[1] as $key1 => $value1){
            $crtData[$key1]['id'] = $key1;
            $crtData[$key1]['name'] = $name[$key1];
            foreach($data[2] as $key2 => $value2){
                if($value2 == $key1){
                    $crtData[$key1]['son'][$key2]['id'] = $key2;
                    $crtData[$key1]['son'][$key2]['name'] = $name[$key2];
                    foreach ($data[3] as $key3 => $value3){
                        if($value3 == $key2){
                            $crtData[$key1]['son'][$key2]['son'][$key3]['id'] = $key3;
                            $crtData[$key1]['son'][$key2]['son'][$key3]['name'] = $name[$key3];
                            foreach ($data[4] as $key4 => $value4){
                                if($value4 == $key3){
                                    $crtData[$key1]['son'][$key2]['son'][$key3]['son'][$key4]['id'] = $key4;
                                    $crtData[$key1]['son'][$key2]['son'][$key3]['son'][$key4]['name'] = $name[$key4];
                                }
                            }
                        }
                    }
                }
            }
        }
                p($crtData);exit;
        return $crtData;
    }

    /**
     * 描述 ：获取类目的商品属性数据
     * 作者 ：Zoro.Zhu
    **/
    public static function getAttr($catId){
        $sql = "SELECT 
            `catId`, `catData`, `addDate`
        FROM
            `pdd_cat_attr`
        WHERE
            `catId` = '{$catId}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：新增类目的商品属性
     * 作者 ：Zoro.Zhu
    **/
    public static function insertCatAttr($attrData, $catId){
        $json = addslashes(json_encode($attrData));
        L::sql("INSERT INTO `pdd_cat_attr`(
          `catId`, `catData`, `addDate`
        ) VALUES (
          '{$catId}', '{$json}', '" . date('Y-m-d H:i:s') . "'
        )");
    }

}
return true;